package studio.raptor.ddal.dashboard.util;

import oracle.sql.TIMESTAMP;

import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

/**
 * Created by liujy on 2018/1/27.
 * 时间工具类
 */
public class DateUtil {

    /**
     * 取当前系统时间,格式:yyyy-MM-dd HH:mm:ss，例如 2018-01-27 13:39:26
     * @return
     */
    public static String getNow() {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sDateFormat.format(new java.util.Date());
    }

    /**
     * 取格式化时间,格式:yyyy-MM-dd HH:mm:ss，例如 2018-01-27 13:39:26
     * @return
     */
    public static String getFormatTimeString(Date date) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sDateFormat.format(date);
    }

    /**
     * 取格式化时间,格式:yyyy-MM-dd HH:mm:ss，例如 2018-01-27 13:39:26
     * @return
     */
    public static String getFormatTimeString(Date date,String datePattern) {
        if(date==null)
            return "";
        SimpleDateFormat sDateFormat = new SimpleDateFormat(datePattern);
        return sDateFormat.format(date);
    }

    public static String getFormatTimeString(Time time, String datePattern) {
        if(time==null) return "";
        SimpleDateFormat sDateFormat = new SimpleDateFormat(datePattern);
        return sDateFormat.format(new Date(time.getTime()));
    }

    public static String getFormatTimeString(Timestamp timestamp,String datePattern) {
        if(timestamp==null) return "";
        SimpleDateFormat sDateFormat = new SimpleDateFormat(datePattern);
        return sDateFormat.format(new Date(timestamp.getTime()));
    }

    public static String getFormatTimeString(TIMESTAMP timestamp, String datePattern) throws SQLException {
        if(timestamp==null) return "";
        SimpleDateFormat sDateFormat = new SimpleDateFormat(datePattern);
        return sDateFormat.format(timestamp.dateValue());
    }

    /**
     * 获取TimeStamp的字符串类型
     * @return
     */
    public static String getTimeString(Date date,String type) {
        String result="";
        if(type!=null&&type.toUpperCase().equals("LONG")&&date!=null){
            result =((Long)date.getTime()).toString();
        }
        return result;
    }

    /**
     * 获取TimeStamp的字符串类型
     * @return
     */
    public static String getTimeStampString(Timestamp timestamp, String type) {
        String result="";
        if(type!=null&&type.toUpperCase().equals("LONG")&&timestamp!=null){
            result =((Long)timestamp.getTime()).toString();
        }
        return result;
    }

    public static void main(String[] args){
        System.out.println( "DateUtil.main:"+ DateUtil.getNow() );
    }

}
